//
//  ZBLiveClassPicker.m
//  ZhiBo
//
//  Created by huangtie on 2017/11/15.
//  Copyright © 2017年 huangtie. All rights reserved.
//

#import "ZBLiveClassPicker.h"

@interface ZBLiveClassPicker()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic , strong) UIPickerView *pickerView;

@property (nonatomic , strong) NSArray <ZBLiveClass *> *liveClassList;

@end

@implementation ZBLiveClassPicker

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.size = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = UICOLOR_RGB(0, 0, 0, 0);
        self.top = SCREEN_HEIGHT;
        
        self.pickerView = [[UIPickerView alloc] init];
        self.pickerView.width = SCREEN_WIDTH;
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
        self.pickerView.bottom = self.height;
        self.pickerView.backgroundColor = UICOLOR_ARGB(0xffffffff);
        [self addSubview:self.pickerView];
        
        UIView *topRect = [[UIView alloc] init];
        topRect.size = CGSizeMake(SCREEN_WIDTH, 40);
        topRect.backgroundColor = UICOLOR_ARGB(0xffffffff);
        topRect.bottom = self.pickerView.top;
        [self addSubview:topRect];
        
        UIButton *done = [UIButton buttonWithType:UIButtonTypeCustom];
        done.size = CGSizeMake(120, 40);
        done.right = topRect.width;
        [done setTitle:@"确认" forState:UIControlStateNormal];
        [done setTitleColor:UICOLOR_ARGB(0xff333333) forState:UIControlStateNormal];
        [done addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
        [topRect addSubview:done];
        
        UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
        cancel.size = CGSizeMake(120, 40);
        [cancel setTitle:@"取消" forState:UIControlStateNormal];
        [cancel setTitleColor:UICOLOR_ARGB(0xff333333) forState:UIControlStateNormal];
        [cancel addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        [topRect addSubview:cancel];
        
    }
    return self;
}

- (void)display:(NSArray <ZBLiveClass *> *)values
{
    self.liveClassList = values;
    [self.pickerView reloadAllComponents];
    
    [UIView animateWithDuration:.3 animations:^{
        self.top = 0;
    }];
}

- (void)hide
{
    [UIView animateWithDuration:.3 animations:^{
        self.top = SCREEN_HEIGHT;
    }];
}

#pragma mark Action
- (void)doneClick
{
    NSInteger index = [self.pickerView selectedRowInComponent:0];
    if (index < self.liveClassList.count) {
        [self.delegate pickerDidSelectClass:self.liveClassList[index]];
        [self hide];
    }
}

- (void)cancelClick
{
    [self hide];
}

#pragma mark Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.liveClassList.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.liveClassList[row].cat_name;
}

@end
